// pages/wxCase/popCustom/index.js
const db = wx.cloud.database()
const Time = require('../../../utils/time/TimeUtil2')
Page({
  data: {
    isShow: true,
    a: {},
    list: [], //数据数组
  },
  show() {
    this.setData({
      isShow: false
    })
  },
  hide() {
    this.setData({
      isShow: true
    })
  },
  //页面加载
  onLoad: function (options) {
    this.getdatabiues()
  },
  async getdatabiues() {
    const MAX_LIMIT = 20
    var value = wx.getStorageSync('autt')
    let c = await db.collection(value.user.shangpin).where({
      df: true
    }).count()
    const total = c.total
    // 计算需分几次取
    const batchTimes = Math.ceil(total / 20)
    // 承载所有读操作的 promise 的数组
    const tasks = []
    for (let i = 0; i < batchTimes; i++) {
      let count = await db.collection(value.user.shangpin).where({
        df: true
      }).skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
      count.data.forEach(element => {
        tasks.push(element)
      });
    }
    this.setData({
      list: tasks
    })
  },
  //点击不同意
  butongyiuuu(e) {
    wx.requestSubscribeMessage({
      tmplIds: ['kC-NoxMdiSMZWexpw72En9bxx41uoDp1g1NrORXfc-w', 'VW3Rknu5JUoik5Rnt50i1fx3Og2M13W0y7q_foGSgU8'],
      success: res => {
        // 授权成功后的处理逻辑
        console.log('用户授权成功', res);
      }
    })
    wx.showModal({
      title: '提示',
      content: '是否接受',
      cancelText: "拒绝",
      confirmText: "接受",
      success: res => {
        if (res.confirm) {
          this.tongyi(e)
          wx.login({
            success: (res) => {
              let ccooddee = res;
              var value = wx.getStorageSync('autt')
              wx.cloud.callFunction({
                name: 'xiaoxitishi', // 这里调用名为login的云开发函数
                data: {
                  user: value,
                  text: "同意",
                  code:ccooddee
                },
                success: res => {
                  console.log(res);
                  if (res.result) {
                    wx.showToast({
                      title: '已通知对方',
                      icon: 'success',
                      duration: 2000
                    })
                  }
                }
              })
            },
          })

        } else if (res.cancel) {
          wx.login({
            success: (res) => {
              console.log('用户点击取消')
              let ccooddee = res;
              this.butongyi(e)
              wx.cloud.callFunction({
                name: 'xiaoxitishi', // 这里调用名为login的云开发函数
                data: {
                  data: {
                    user: value,
                    text: "拒绝",
                    code:ccooddee
                  }
                },
                success: res => {
                  console.log(res);
                  if (res.result) {
                    wx.showToast({
                      title: '通知对方',
                      icon: 'success',
                      duration: 2000
                    })
                  }
                }
              })
            },
          })

        }
      }
    })
  },
  //不同意
  async butongyi(e) {
    // if(e.e.currentTarget.dataset.from.){}
    wx.showLoading({
      title: '马上~~',
      mask: true
    })
    console.log(e);
    let ARr = this.data.list
    let data = e.currentTarget.dataset.from
    let index = e.currentTarget.dataset.inde
    console.log(index);
    ARr[index].df = false;
    console.log(ARr);
    var value = wx.getStorageSync('autt')
    let t = Time.formatDatetime(new Date)
    let str = value.user.aut + '拒绝' + '时间:' + t
    let sttr = value.user.aut + '拒绝使用' + data.name
    await db.collection(value.user.shangpin).doc(data._id).update({
      data: {
        df: false,
        id: str,
        jujue: true,
      },
    })
    // boole = true
    this.setData({
      list: ARr
    })
    this.openDemJF(-8, sttr)
    // 增加积分
    //去对方购物车设置
    console.log(data);
    await db.collection(value.user.cpgouwuche).doc(data.gouwucheid).update({
      data: {
        df: false,
        id: str,
        jujue: true,
      },
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 2000)
    // console.log(e.currentTarget.dataset.from);
  },
  //增加积分
  async openDemJF(num, string) {
    var value = wx.getStorageSync('autt')
    console.log("jifebn");
    //连接数据库\
    let uup = db.command
    let tishi = '积分+' + num
    let bz = string + '+' + num
    const _ = db.command
    //获取当前时间
    let tim = Time.getDangQianRiQi()
    let datad = Time.formatDatetime(tim)
    let dx = {
      nr: bz,
      time: datad
    }
     //获取本地数据
     let ve = wx.getStorageSync('autt')
     let arres = ve.user.jfmingxi
    if(arres.length>50){arres=arres.slice(0,50)}
    arres.unshift(dx)
    await db.collection('users').doc(value.user._id).update({
      // data 传入需要局部更新的数据
      data: {
        jf: uup.inc(num),
        jfmingxi:arres
      },
      success: function (res) {
        console.log("++++>");
      }

    })
  },
  //点击同意
  async tongyi(e) {
    // if(e.e.currentTarget.dataset.from.){}
    wx.showLoading({
      title: '马上~~',
      mask: true
    })
    console.log(e);
    let ARr = this.data.list
    let data = e.currentTarget.dataset.from
    let index = e.currentTarget.dataset.inde
    console.log(index);
    ARr[index].df = false;
    console.log(ARr);
    var value = wx.getStorageSync('autt')
    let t = Time.formatDatetime(new Date)
    let str = value.user.aut + '已同意' + '时间:' + t
    let sttr = value.user.aut + '同意使用' + data.name
    await db.collection(value.user.shangpin).doc(data._id).update({
      data: {
        df: false,
        id: str,
      },
    })
    // boole = true
    this.setData({
      list: ARr
    })
    this.openDemJF(5, sttr)
    // 增加积分
    //去对方购物车设置
    console.log(data);
    await db.collection(value.user.cpgouwuche).doc(data.gouwucheid).update({
      data: {
        df: false,
        id: str,
      },
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 2000)
    // console.log(e.currentTarget.dataset.from);
  },
  //增加积分
  async openDemJF(num, string) {
    var value = wx.getStorageSync('autt')
    console.log("jifebn");
    //连接数据库\
    let uup = db.command
    let tishi = '积分+' + num
    let bz = string + '+' + num
    const _ = db.command
    //获取当前时间
    let tim = Time.getDangQianRiQi()
    let datad = Time.formatDatetime(tim)
    let dx = {
      nr: bz,
      time: datad
    }
     //获取本地数据
     let ve = wx.getStorageSync('autt')
     let arres = ve.user.jfmingxi
    if(arres.length>50){arres=arres.slice(0,50)}
    arres.unshift(dx)
    await db.collection('users').doc(value.user._id).update({
      // data 传入需要局部更新的数据
      data: {
        jf: uup.inc(num),
        jfmingxi: arres
      },
      success: function (res) {
        console.log("++++>");
      }

    })
  },

})